// pages/audio/index.js
const api = require('../../utils/api')
const util = require('../../utils/util.js')
const app = getApp()
const innerAudioContext = wx.createInnerAudioContext() //创建音频实例

Page({

    /**
   * 页面的初始数据
   */
    data: {
        image:app.globalData.image,//课程封面图
        courseid:0,//课程id
        audioData:null,
        playing: false, //是否在播放
        progress: 0, //进度
        totalTime: 0, //音频总时长
        formatTotalTime: '00:00', //音频总时长
        currentTime: 0, //当前时长
        formatCurrentTime: '00:00', //当前时长
        dragging:false,//正在拖拽进度条
    },
    /**
   * 完成一次拖动后
   */
    sliderchange: function(e) {
    },
    /**
   * 拖动过程中触发
   */
    sliderchanging: function(e) {
        let currentTime = (e.detail.value*this.data.totalTime)/100
        innerAudioContext.seek(currentTime)
        this.setData({
            currentTime,
        })
    },
    /**
   * 快进15秒
   */
    diver:function(){
        if(!this.data.playing)return
        let currentTime = this.data.currentTime+15,
            playing = currentTime<=this.data.totalTime?true:false
        innerAudioContext.seek(currentTime)
        this.setData({
            currentTime,
            playing
        })
    },
    /**
   * 快退15秒
   */
    return:function(){
        if(!this.data.playing)return
        let currentTime = this.data.currentTime - 15<=15?0:this.data.currentTime - 15
        innerAudioContext.seek(currentTime)
        this.setData({
            currentTime
        })
    },
    /**
   * 开始播放
   */
    play: function() {
        innerAudioContext.play()
        this.setData({
            playing: true
        })
    },
    /**
   * 暂停播放
   */
    pause: function() {
        innerAudioContext.pause()
        this.setData({
            playing: false
        })
    },
    /**
   * 音频相关事件处理
   */
    audioEventsHandle: function() {


        innerAudioContext.src = util.relative2absolute(this.data.audioData.audio) //src赋值

        let progress,currentTime,formatCurrentTime,totalTime,formatTotalTime

        /*监听音频进入可以播放状态*/
        innerAudioContext.onCanplay((e) => {
            progress = Math.floor((innerAudioContext.currentTime / innerAudioContext.duration) * 100)
            currentTime = Math.floor(innerAudioContext.currentTime)
            formatCurrentTime = util.secondToDate(Math.floor(innerAudioContext.currentTime))
            totalTime = Math.floor(innerAudioContext.duration)
            //formatTotalTime = util.secondToDate(Math.floor(innerAudioContext.duration))

            this.setData({
                progress,
                currentTime,
                formatCurrentTime,
                totalTime,
            })
        })
        /*播放监听*/
        innerAudioContext.onPlay(() => {
            console.log('开始播放')
        })
        /*错误监听*/
        innerAudioContext.onError((res) => {
            console.log(res.errMsg)
        })
        /*监听音频播放进度更新*/
        innerAudioContext.onTimeUpdate(() => {

            progress = Math.floor((innerAudioContext.currentTime / innerAudioContext.duration) * 100)
            currentTime = Math.floor(innerAudioContext.currentTime)
            formatCurrentTime = util.secondToDate(Math.floor(innerAudioContext.currentTime))
            totalTime = Math.floor(innerAudioContext.duration)
            formatTotalTime = util.secondToDate(Math.floor(innerAudioContext.duration))
            this.setData({
                progress,
                currentTime,
                formatCurrentTime,
                totalTime,
                formatTotalTime,
            })

            // 播放结束
            if (progress >= 100){
                this.setData({
                    playing:false,
                    progress:0
                })
            }
        })

    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        let id = options._id,
            courseid = options.courseid
        //请求课程数据
        api.post('/class/class',{
            id,
        }).
        then(res => {
            let formatTotalTime = util.secondToDate(res.data.classTime)
            this.setData({
                formatTotalTime,
                audioData:res.data,
                courseid,
                image:app.globalData.image
            })
            app.globalData.article = res.data.article//全局文稿储存
            this.audioEventsHandle() //音频相关事件处理
        })

    },
    /***
   *路由跳转
   */
    routerPush: function (e) {

        let url = e.currentTarget.dataset.link
        wx.navigateTo({
            url,
        })
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {

    },

    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {

    },

    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {

    },

    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {

    },

    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {

    },

    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {

    },

    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {

    }
})